package com.cbs.java.component.application;

import org.fusesource.jansi.Ansi;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;

/**
 * Created by broche on 31/08/2017.
 */
public abstract class Application {

    public abstract String applicationName();

    public abstract BaseArgs getArgs();

    private Configs configs;

    private String[] args;

    private boolean debug = false;

    public boolean isDebug() {
        return debug;
    }

    public Application(String[] args) {
        this.args = args;
        configs = new Configs(this, "config.properties");

        debug = getProperty("Debug", false);

    }

    public Configs getConfigs() {
        return configs;
    }

    public void start() {
        CmdLineParser parser = new CmdLineParser(getArgs());
        try {
            parser.parseArgument(args);
        } catch (CmdLineException e) {
            e.printStackTrace();
            return;
        }

        Ansi.setEnabled(!getArgs().isNocolor());
    }

    public String getProperty(String key, String defaultValue) {
        return getConfigs().getProperty(key, defaultValue);
    }

    public int getProperty(String key, int defaultValue) {
        return getConfigs().getProperty(key, defaultValue);
    }

    public float getProperty(String key, float defaultValue) {
        return getConfigs().getProperty(key, defaultValue);
    }

    public boolean getProperty(String key, boolean defaultValue) {
        return getConfigs().getProperty(key, defaultValue);
    }
}
